package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.sfrobot.entity.Prescription;
import com.lizhi.yun.db.sfrobot.repository.PrescriptionRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/12/15 12:26
 */
@Slf4j
@Service
public class RecvPrescriptionHandler {

    @Resource
    private PrescriptionRepo prescriptionRepo;

    public String recvPrescription(String json) {
        UpDataPOJO upClientPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upClientPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)) {
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*上传的数据信息信息*/
        Prescription prescription = SamJsonUtil.toObject(upClientPOJO.getData(), Prescription.class);
        if (StringUtils.isEmpty(prescription)) {
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        Prescription example = new Prescription();
        example.setId(prescription.id);
        example.setHospitalId(rpcClientPOJO.getOrganId());
        example.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        Prescription recordFromDb = prescriptionRepo.findOne(Example.of(example)).orElse(null);
        if (StringUtils.isEmpty(recordFromDb)) {
            prescription.setLid(StringUtils.newId());
        } else {
            prescription.setLid(recordFromDb.lid);
        }
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        prescription.setCreateTime(DateUtils.getNowDate());
        prescription.setHospitalId(rpcClientPOJO.getOrganId());
        prescription.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        log.info("上传的数据信息是prescription:{}", prescription);
        prescriptionRepo.save(prescription);
        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
